package cc.customer.Conversation.impl;

import cc.customer.Conversation.ConversationService;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.DateUtil;
import cc.customer.common.utils.Utils;
import cc.customer.myCustomer.damain.CusResource;
import cc.customer.myCustomer.damain.MyCustomer;
import cc.customer.myCustomer.dao.CusResourceMapper;
import cc.customer.myCustomer.dao.MyCustomerMapper;
import cc.customer.rabbit.dao.CconvNoticeMapper;
import cc.customer.rabbit.domain.CconvNotice;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ConversationServiceImpl extends BaseService<CconvNotice> implements ConversationService {

   private static final Logger log = LoggerFactory.getLogger(CconvNotice.class);

   @Autowired
   private CconvNoticeMapper ccmapper;
   @Autowired
   private MyCustomerMapper mcsMapper;
    @Autowired
    private CusResourceMapper cusResourceMapper;
   @Autowired
   private UserService userService;



   @Override
   public ResponseBo selectMyNotice(MyUser user){
      try{
         String userid = user.getUsername();
         String deptid = user.getDeptId()+"";
         List<CconvNotice> list = ccmapper.selectAllNotice(userid,deptid);
         return ResponseBo.ok("查询成功",list);
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error();
      }
   }

   @Override
   @Transactional
   public ResponseBo updateShare(String uuid,String data,String session,String cid,MyUser user)throws RuntimeException{
      try{
         CconvNotice notice = new CconvNotice();
         notice.setcConvSession(Utils.getUUID());
         notice.setcConvTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
         notice.setcConvStatus("0");
         notice.setcConvType("1");
         notice.setcConvNType("4");
         notice.setcConvTitle("共享客户");
         notice.setcConvCreator(user.getUserUuid());
         String username = userService.findUserByUuids(cid).getUsername();
         notice.setcConvUsers(username);
         /**更新共享客户表*/
         MyCustomer customer = mcsMapper.selectMyCuatomerDetial(uuid);
         String shares = customer.getMyCusShare();
         String newShare = "";
         if(shares==null||"".equals(shares)){
            newShare = ","+cid;
         }else{
            newShare = shares+","+cid;
         }
         MyCustomer cus = new MyCustomer();
         cus.setMyCusId(Long.parseLong(uuid));
         cus.setMyCusUuid(customer.getMyCusUuid());
         cus.setMyCusShare(newShare);
         List<MyCustomer> list = new ArrayList<>();
         list.add(cus);
         if("1".equals(data)){
            mcsMapper.updateCusBatchContactMine(list);
         }
         /**更新通知消息状态*/
         Example example = new Example(CconvNotice.class);
         example.createCriteria().andEqualTo("cConvSession",session);
         CconvNotice odnot = this.selectByExample(example).get(0);
         String content = odnot.getcConvCont();
         JSONObject json = JSONObject.parseObject(content);
         String customername = json.getString("customername");
         if("1".equals(data)){
            json.put("ntype","3");
         }else{
            json.put("ntype","4");
         }
         String newCont = json.toJSONString();
         ccmapper.updateContentBySession(session,newCont);

         /**发送通知申请人*/
         JSONObject json1 = new JSONObject();
         json1.put("custype","4");
         json1.put("customername",customername);
         json1.put("ntype","5");
         json1.put("customerid",uuid);
         if("1".equals(data)){
            json1.put("content","您向 "+user.getNickname()+" 申请共享的客户 "+customername+" 通过");
         }else{
            json1.put("content","您向 "+user.getNickname()+" 申请共享的客户 "+customername+" 被拒绝");
         }
         notice.setcConvCont(json1.toJSONString());
        // rabbitMqSender.sendDirect(notice);
         return ResponseBo.ok();
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error();
      }
   }

   /**选择通知类型*/
   @Override
   public ResponseBo selectNoticeByType(String username,String type){
      List<CconvNotice> list = ccmapper.selectByNType(username,type);
      return ResponseBo.ok("",list);
   }

   @Override
   public ResponseBo selectMonthRecord(CconvNotice notice){
      Example example = new Example(CconvNotice.class);
      example.createCriteria().andEqualTo("cConvSession",notice.getcConvSession());
      CconvNotice newNo =  this.selectByExample(example).get(0);
      if(newNo!=null){
         return ResponseBo.ok("查询成功",newNo);
      }else{
         return ResponseBo.error("查询失败");
      }
   }

   /**查通知详情*/
   @Override
   public List<CconvNotice> selectNoticeDetail(CconvNotice notice){
      try{
         Example example = new Example(CconvNotice.class);
         example.createCriteria().andEqualTo("cConvSession",notice.getcConvSession());
         CconvNotice newNo  = this.selectByExample(example).get(0);
         String conts = newNo.getcConvCont();
         JSONObject json = JSONObject.parseObject(conts);
         String[] types = json.getString("custype").split(",");
         String[] cusid = json.getString("customerid").split(",");
         String[] cusname = json.getString("customername").split(",");
         String[] cusdeids = json.getString("cusid").split(",");
         List<CconvNotice> list = new ArrayList<>();
         if(types.length>1){
            for(int i = 0;i<types.length;i++){
               CconvNotice cus = new CconvNotice();
               cus.setcConvType(types[i]);
               cus.setcConvSession(cusid[i]);
               cus.setcConvUsers(cusname[i]);
               cus.setcConvId(Integer.valueOf(cusdeids[i]));
               list.add(cus);
            }
         }else{
            for(int i = 0;i<cusid.length;i++){
               CconvNotice cus = new CconvNotice();
               cus.setcConvType(json.getString("custype"));
               cus.setcConvUsers(cusname[i]);
               cus.setcConvSession(cusid[i]);
               cus.setcConvId(Integer.valueOf(cusdeids[i]));
               list.add(cus);
            }
         }
         return list;
      }catch (Exception e){
         e.printStackTrace();
         log.error(e.getMessage());
         return new ArrayList<>();
      }
   }

   /**导出客户资源**/
   @Override
   public List<CusResource> exportSessionData(String nsessionId) {
        try {
            Example example = new Example(CconvNotice.class);
            example.createCriteria().andEqualTo("cConvSession",nsessionId);
            CconvNotice newNoOne  = this.selectByExample(example).get(0);
            String conts = newNoOne.getcConvCont();
            JSONObject json = JSONObject.parseObject(conts);
            String[] cusUuid = json.getString("customerid").split(",");
            Example example1 = new Example(CusResource.class);
            example1.createCriteria().andIn("uuid", Arrays.asList(cusUuid));
            List<CusResource> emp =  cusResourceMapper.selectByExample(example1);
            return emp;
        }catch (Exception e){
            log.error("导出会话的资源列表失败");
            return new ArrayList<>();
        }

   }
}
